<?php
($user_id = session('user_id')) || abort('请先登录');
$page = _POST('page', 1);
$page_size = _POST('page_size', 10);
return GZ_get_collection_goods($user_id, $page, $page_size);

/**
 *  获取指定用户的收藏商品列表
 *
 * @access  public
 * @param   int     $user_id        用户ID
 * @param   int     $num            列表最大数量
 * @param   int     $start          列表其实位置
 *
 * @return  array   $arr
 */
function GZ_get_collection_goods($user_id, $page = 1, $page_size = 10)
{
    $start = ($page-1)*$page_size;
    $limit = $page_size+1;

    $sql = 'SELECT g.original_img,g.goods_id, g.goods_name, g.market_price, g.shop_price, g.goods_thumb, g.goods_img, g.original_img, g.goods_brief, g.goods_type AS org_price, '.
                "IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS shop_price, ".
                'g.promote_price, g.promote_start_date,g.promote_end_date, c.rec_id, c.is_attention' .
            ' FROM ' . $GLOBALS['ecs']->table('collect_goods') . ' AS c' .
            " LEFT JOIN " . $GLOBALS['ecs']->table('goods') . " AS g ".
                "ON g.goods_id = c.goods_id ".
            " LEFT JOIN " . $GLOBALS['ecs']->table('member_price') . " AS mp ".
                "ON mp.goods_id = g.goods_id AND mp.user_rank = '$_SESSION[user_rank]' ".
            " WHERE c.user_id={$user_id} ORDER BY c.rec_id DESC LIMIT {$start},{$limit}";

    $res = $GLOBALS['db']->getAll($sql);
    $has_more = 0;
    if (count($res) > $page_size)
    {
        $has_more = 1;
        array_pop($res);
    }
    $goods_list = array();
	if ($res)
    {
	    foreach ($res as $row)
        {
            if ($row['promote_price'] > 0)
            {
                $promote_price = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
            }
            else
            {
                $promote_price = 0;
            }
            $goods_list[] = [
                'rec_id'        => $row['rec_id'],
                'is_attention'  => $row['is_attention'],
                'goods_id'      => $row['goods_id'],
                'goods_name'    => $row['goods_name'],
                'market_price'  => price_format($row['market_price']),
                'shop_price'    => price_format($row['shop_price']),
                'promote_price' => ($promote_price > 0) ? price_format($promote_price) : '',
                'url'           => build_uri('goods', array('gid'=>$row['goods_id']), $row['goods_name']),
                'original_img'  => API_DATA('PHOTO', $row['original_img']),
                'goods_img'     => API_DATA('PHOTO', $row['goods_img']),
                'goods_thumb'   => API_DATA('PHOTO', $row['goods_thumb']),
                'goods_brief'   => $row['goods_brief'],
                'goods_type'    => $row['goods_type'],
            ];
	    }
	}

    return ['has_more' => $has_more, 'list' => $goods_list];
}
